package algorithm.leetcode;

import algorithm.templates.ListNode;

public class NO142_环形链表II {
    class Solution {
        public ListNode detectCycle(ListNode head) {
            if (head == null || head.next == null) {
                return null;
            }
            ListNode slow = head, fast = head;
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
                if (slow == fast) {
                    ListNode temp = head;
                    while (temp != slow) {
                        slow = slow.next;
                        temp = temp.next;
                    }
                    return temp;
                }
            }
            return null;
        }
    }
}
